import { defineStore } from 'pinia';
import service from '/@/utils/request';
import { query } from '/@/utils/params';
import { Account } from '../../system/account/account';
import { easyDate } from '/@/utils/formatTime';

export interface MaintainerVo {
	id: string;
	name: string; //
	tel: string; //
	contactName: string; // 联系人
	contactTel: string; // 联系电话
	dealerId: string;
	companyId: string;
	address: string; // 地址
	account: string; // 账号
	password: string; // 密码
}
export interface MaintainerDTO {
	id: string;
	name: string; //
	tel: string; //
	account: Account; // 账号
	password: string; // 密码
	createdAt: string;
	company: any;
	dealer: any;
	branch: any;
	status: string;
}
export const useMaintainer = defineStore('maintainer', {
	actions: {
		async getMaintainerList(name: string, pageSize: number, pageNumber: number) {
			return service.get(query('/api/maintainer', { name, pageSize, pageNumber })).then((res) => {
				res.list = res.list.map((c: MaintainerDTO) => {
					return {
						...c,
						account: c.account.account,
						company: c.company.name,
						companyId: c.company.id,
						dealer: c.dealer.name,
						dealerId: c.dealer.id,
						branch: c.branch.name,
						branchId: c.branch.id,
						createdAt: easyDate(c.createdAt),
					};
				});
				return res;
			});
		},

		async saveMaintainer(vo: MaintainerVo) {
			return service.post('/api/maintainer/create', vo);
		},
		/**
		 * 编辑
		 */
		async updateMaintainer(vo: MaintainerVo) {
			return service.patch(`/api/maintainer/${vo.id}`, vo);
		},

		async delete(id: string) {
			return service.delete(`/api/maintainer/${id}`);
		},
		async toggoleStatus(id: string, status: number) {
			return service.patch(`/api/maintainer/${id}/status?status=${status}`);
		},
	},
});
